<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Model\{Admin,Config}; 

class Article extends Model
{
	protected $table = 'article';
	protected $primaryKey = 'id';
	public $timestamps = false;


	public static function article_list($obj,$words=[]){
		$list = $obj->paginate(20,['id']);
		$cats = SELF::article_cat();
		$vide_cats = SELF::video_cat();
		$preg = [];

		if(is_array($words)) foreach($words as $word=>$line){
			$preg['#'.$word.'#i'] = function($match){
				return "<strong>".$match[0]."</strong>";
			};
		}
		$grab = Config::get_config_bykey('article_grab');
		foreach($list->items() as &$item){
			$item->pic = empty($item->pic)?[]:explode(',',$item->pic);
			if($item->type_of_article==3){
				$item->cat_name = $vide_cats[$item->type]->name??'unfind';
			}else{
				$item->cat_name = $cats[$item->type]->name??'unfind';
			}
			
			if(!empty($words) && !preg_match('#script#i', $item->title)){
				$item->format_title = preg_replace_callback_array($preg,$item->title);
			}
			$item->grab = $grab[$item->grab]??'unfind';
		}
		return $list;
	}

	public static function line_article_list($obj,$words=[]){
		$list = $obj->paginate(20,['id']);
		$cats = SELF::article_cat();
		$vide_cats = SELF::video_cat();
		$preg = [];

		if(is_array($words)) foreach($words as $word=>$line){
			$preg['#'.$word.'#i'] = function($match){
				return "<strong>".$match[0]."</strong>";
			};
		}
		
		$ids = [];
		$grab = Config::get_config_bykey('article_grab');
		foreach($list->items() as &$item){
			$ids[] = $item->id;
			$item->pic = empty($item->pic)?[]:explode(',',$item->pic);
			if($item->type_of_article==3){
				$item->cat_name = $vide_cats[$item->type]->name??'unfind';
			}else{
				$item->cat_name = $cats[$item->type]->name??'unfind';
			}
			
			if(!empty($words) && !preg_match('#script#i', $item->title)){
				$item->format_title = preg_replace_callback_array($preg,$item->title);
			}
			$item->grab = $grab[$item->grab]??'unfind';
		}
		$admins = [];
		$temp = DB::table('check_article_queue')->whereIn('aid',$ids)->select(['admin_id','aid'])->get()->toArray();
		foreach($temp as $t){
			$admins[$t->aid] = $t->admin_id;
		}
		$users = DB::table('admindata')->whereIn('id',array_values($admins))->get()->toArray();
		$users = arrays_set_key($users,'id');
		foreach($list->items() as &$row){
			if(isset($admins[$row->id])){
				$row->admin_name = $users[$admins[$row->id]]->username??'';
			}else{
				$row->admin_name = '';
			}
			
		}
		return $list;
	}

	public static function video_list($obj,$words=[]){
		DB::connection()->enableQueryLog();
		$list = $obj->paginate(20,['id']);
		 #print_r(DB::getQueryLog()); exit;
		$cats = SELF::video_cat();

		if(is_array($words)) foreach($words as $word=>$line){
			$preg['#'.$word.'#i'] = function($match){
				return "<strong>".$match[0]."</strong>";
			};
		}
		$grab = Config::get_config_bykey('article_grab');
		foreach($list->items() as &$item){
			$item->pic = empty($item->pic)?[]:explode(',',$item->pic);
			$item->cat_name = $cats[$item->type]->name??'unfind';
			if(!empty($words) && !preg_match('#script#i', $item->title)){
				$item->format_title = preg_replace_callback_array($preg,$item->title);
			}
			$item->grab = $grab[$item->grab]??'unfind';
		}
		return $list;
	}

	public static function info($p){
		return DB::connection('db2')->table('article')->where('id',$p['id'])->first();
	}

	public static function article_cat(){
		$rs = DB::table('typedata')->get();
		return arrays_set_key($rs,'id');
	}

	public static function video_cat(){
		$rs = DB::table('typedata_video')->get();
		return arrays_set_key($rs,'id');
	}

	public static function video_info($p){
		return DB::table('video')->where('id',$p['id'])->first();
	}

	/**
	 * $p['admin_id']
	 * $p['name']
	 * $p['article_id']
	 * $p['type'] 0文章，1视频
	 * $p['status']
	 * @return [type] [description]
	 */
	public static function log($p){
		$article_id = $p['article_id'];
		$p['add_time'] = time();
		$p['ymd'] = date('Ymd',$p['add_time']);
		$p['ym'] = date('Ym',$p['add_time']);
		
		if($p['type']==0){
			$info = DB::connection('db2')->table('article')->where('id',$p['article_id'])->first(['type']);
		}else{
			$info = DB::connection('db2')->table('video')->where('id',$p['article_id'])->first(['type']);
		}
		
		
		$p['cat_id'] = $info->type;
		$p['desc'] = $p['status']==1?"通过[$article_id]":"拒绝[$article_id]";
		$p['desc'] = $p['name'].' '.$p['desc'];
		$p['admin_name'] = $p['name'];
		unset($p['name']);
		DB::table('check_log')->insert($p);
	}


	/**
	 * 加入上线计划
	 * $p['type'] 0文章 1队列
	 * $p[article_id] 文章id
	 * $p[set_time] 上线时间 默认0
	 */
	public static function add_queue($p){
		if(!isset($p['type']) || empty($p['article_id'])){
			return false;
		}
		$p['add_time'] = time();
		$p['exec'] = 0;
		return DB::table('check_article_queue')->insert($p);
	}

	/**
	 * 排队中的文章
	 */
	 public static function article_queue(){
	 	$list = DB::table('check_article_queue')->select(['*'])->where([
	 		['exec','=',0],
	 		['type','=',0]
	 	])->paginate(20,['id']);

	 	$ids = get_array_keys($list->items(),'article_id');
	 	$admin_ids = get_array_keys($list->items(),'admin_id');
	 	$articles = DB::connection('db2')->table('article')->select('id','type','title','pic','ar_source')->whereIn('id',$ids)->get();
	 	$admins = DB::table('admindata')->select('id','username')->whereIn('id',$admin_ids)->get();;
	 	$admins = arrays_set_key($admins,'id');
	 	
	 	$cats = SELF::article_cat();
	 	$data = [];
	 	foreach($articles as $item){
	 		$item->pic = empty($item->pic)?[]:explode(',',$item->pic);
			$item->cat_name = $cats[$item->type]->name??'unfind';
			$data[$item->id] = $item;
	 	}
	 	$grab = Config::get_config_bykey('article_grab');
	 	foreach($list->items() as &$line){
	 		$line->pic = $data[$line->article_id]->pic??[];
	 		$line->title = $data[$line->article_id]->title??'';
	 		$line->cat_name =$data[$line->article_id]->cat_name??'';
	 		$line->ar_source  = $data[$line->article_id]->ar_source??0;
	 		$line->username =  $admins[$line->admin_id]->username??'unfind';
	 		#$line->grab = $grab[$line->grab]??'unfind';
	 	}
	 	return [
	 		'list'=>$list,
	 		'articles'=>$data,
	 	];
	 }

	 /**
	 * 排队中的视频
	 */
	 public static function video_queue(){
	 	$list = DB::table('check_article_queue')->select(['*'])->where([
	 		['exec','=',0],
	 		['type','=',1]
	 	])->paginate(20,['id']);

	 	$ids = get_array_keys($list->items(),'article_id');
	 	$admin_ids = get_array_keys($list->items(),'admin_id');
	 	$articles = DB::connection('db2')->table('video')->select('id','type','title','pic','ar_source')->whereIn('id',$ids)->get();
	 	$admins = DB::table('admindata')->select('id','username')->whereIn('id',$admin_ids)->get();;
	 	$admins = arrays_set_key($admins,'id');
	 	
	 	$cats = SELF::video_cat();
	 	$data = [];
	 	foreach($articles as $item){
	 		$item->pic = empty($item->pic)?[]:explode(',',$item->pic);
			$item->cat_name = $cats[$item->type]->name??'unfind';
			$data[$item->id] = $item;
	 	}
	 	$grab = Config::get_config_bykey('article_grab');
	 	foreach($list->items() as &$line){
	 		$line->pic = $data[$line->article_id]->pic??[];
	 		$line->title = $data[$line->article_id]->title??'';
	 		$line->cat_name =$data[$line->article_id]->cat_name??'';
	 		$line->ar_source  = $data[$line->article_id]->ar_source??0;
	 		$line->username =  $admins[$line->admin_id]->username??'unfind';
	 		#$line->grab = $grab[$line->grab]??'unfind';
	 	}
	 	return [
	 		'list'=>$list,
	 		'articles'=>$data,
	 	];
	 }
}